package com.google.webCash.client;

import com.google.gwt.user.client.Timer;
import com.gwtext.client.widgets.Component;
import com.gwtext.client.widgets.Container;
import com.gwtext.client.widgets.PaddedPanel;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.TabPanel;
import com.gwtext.client.widgets.event.TabPanelListenerAdapter;
import com.gwtext.client.widgets.layout.ColumnLayout;
import com.gwtext.client.widgets.layout.ColumnLayoutData;

public class MainPanel extends TabPanel {

	public static MainPanel self = null;
	public static Panel welcome = null;
	public static Panel camera = null;
	public Panel cameraList0 = null;
	private static Timer resh;
	
	public MainPanel(){
	}
	
	public static MainPanel getInstacne(){
		if(self == null){
			self = new MainPanel();
			creat();
		}
		return self;
	}
	
	private static void creat() {
		if(welcome==null){
			creatWelcome();
		}
		self.add(welcome);
		self.activate(0);
//		self.setWidth("100%");
//		self.setAutoWidth(true);
//		Panel main = new Panel();
//		main.setId("main");
//		main.setHtml("<p>center panel</p>");

//		self.setBodyStyle("background-color:#C3D9FF");
//		self.add(main);
		
		self.addListener(new TabPanelListenerAdapter() {
			public void onClose(Panel panel) {
				int size = self.getComponents().length;
				System.out.println(size);
			}
			public void onTabChange(TabPanel source, Panel tab) {
				System.out.println(tab.getId());
				if(camera != null) {
					if(!tab.getId().equals(camera.getId())){
						if(resh!=null){
							resh.cancel();
							MessageConsole.setText("终止获取数据");
						}
					} else {
						resh.run();
					}
				}
			}

			public void onRemove(Container self, Component component) {
				component.hide();
			}
		});
	}
	
	private static void creatWelcome() {
		welcome = new Panel();
		welcome.setTitle("欢迎");
//		tab.setIconCls("tab-icon");
		welcome.setHtml("Tab Body ");
		welcome.setId("welcome");
//		welcome.setClosable(true);
	}

	public void show0(){
		if(resh!=null){
			resh.cancel();
			MessageConsole.setText("终止获取数据");
		}
		self.setActiveItemID("welcome");
//		self.setHtml("<p>center panel</p>");
//		Component body = self.getComponent("main");
//		if(body!=null){
//			self.remove("main");
//		}
		
		
//		self.add(main);//, mainData);
//		Viewport port = new Viewport(self);
	}
	
	int index = 0;
	public void show1(){
		if (camera != null) {
			Component temp = self.getComponent(camera.getId());
			if (temp != null) {
				self.activate(camera.getId());
				return;
			}
		}
		camera = new Panel();
		camera.setId("main");
		camera.setLayout(new ColumnLayout());
		camera.setTitle("监控");
		camera.setClosable(true);
		
		cameraList0 = new Panel();
		cameraList0.setTitle("已上线列表");
		cameraList0.setHeight(self.getHeight()-40);

		Panel eastPanel0 = new Panel();
		eastPanel0.setTitle("camera");
		eastPanel0.setHeight(self.getHeight()-40);
		
		Panel cameraList = new PaddedPanel(cameraList0, 5, 0, 5, 5);
		Panel eastPanel = new PaddedPanel(eastPanel0, 5, 5, 5, 5);
		
		camera.add(eastPanel, new ColumnLayoutData(.8));
		camera.add(cameraList, new ColumnLayoutData(.2));
		
		self.add(camera);
		
//		if(camera == null){
			
			
//			self.add(camera);
//			self.setActiveItemID("main");
			
//			BorderLayoutData mainData = new BorderLayoutData(RegionPosition.CENTER);
//			mainData.setMargins(new Margins(0, 0, 5, 0));
//		}
		
		resh = new Timer(){
			public void run() {
				cameraList0.setHtml("timer is :" + index);
				MessageConsole.setText(Integer.toString(index));
				index+=1;
				long last = 0;
				long ti = System.currentTimeMillis() - last;
				int sc;
				if( ti > 500 ) {
					sc = 500;
				} else {
					sc = (int)ti + 1;
				}
				last = System.currentTimeMillis();
				if( resh != null){
					resh.schedule(sc);
				}
			}
		};
		resh.run();
		self.activate(camera.getId());
	}
	
	public void show2(){
		if(resh!=null){
			resh.cancel();
		}
//		Component body = self.getComponent("main");
//		if(body!=null){
//			self.remove("main");
//		}
//		self.setHtml("<p>center panel2</p>");
	}
}
